// pages/01homePage/02openTable/openTable.js
var app = getApp()
var appData = getApp().globalData
var _this
Page({
   data: {
      cateBtnStateArr: [],
      goodsList: [],
      tableInfo: '',
      foodOrderList: [],
      foodNum: 0,
      totalPrice: 0,
      msg_show: 0,
   },

   onLoad: function (opt) {
      _this = this
      var obj = JSON.parse(opt.table);
      // 设置scroll-view高度
      this.setData({
         win_h: wx.getSystemInfoSync().windowHeight-48,
         tableInfo: obj
      })
      // 设置bar标题
      wx.setNavigationBarTitle({
         title: appData.shopInfo.Biz_Name + ' ' + obj.fenlei + obj.zhuo,
      })
      // 获取商家自定义分类数据
      var c_list = appData.goodsCateList
      if (c_list) {
         this.setData({//页面用分类数据，如果全局里有就不请求
            goodsCateList: c_list
         })
         // 获取商品分类数据
         var sort = this.data.goodsCateList
         var clickID = sort[0].Category_ID
         this.getGoodsData(clickID)
      } else {
         var that = this
         this.handleCateData()
      }
   },
   //菜谱上加
   addFoodMenu: function (e) {
      var f_list = this.data.foodOrderList
      var goodsInfo = this.data.goodsList[e.currentTarget.id]
      var index = this.isExistAtOrder(goodsInfo)
      if (index == -1) {
         var food = {
            id: goodsInfo.Products_ID,
            name: goodsInfo.Products_Name,
            pic: goodsInfo.Product_homeImg,
            price: goodsInfo.Products_PriceX,
            // jjdangwei: goodsInfo.jjdangwei,
            num: 1
         }
         f_list.push(food)
         // console.log("【获取添加的商品】===", goodsInfo)
      } else {
         f_list[index].num++
      }
      this.setData({ foodOrderList: f_list })
      this.countNumAndPrice()
      this.goodsListAddNum()
   },
   // 订单里加
   addFoodOmenu: function (e) {
      var f_list = this.data.foodOrderList
      var foodsInfo = this.data.foodOrderList[e.currentTarget.id]
      var index = this.isExistAtOrder(foodsInfo)
      if (index == -1) {
         var food = {
            id: foodsInfo.Products_ID,
            name: foodsInfo.Products_Name,
            pic: foodsInfo.Product_homeImg,
            price: foodsInfo.Products_PriceX,
            // jjdangwei: foodsInfo.jjdangwei,
            num: 1
         }
         f_list.push(food)
      } else {
         f_list[index].num++
      }
      this.setData({ foodOrderList: f_list })
      this.countNumAndPrice()
      this.goodsListAddNum()
      // console.log('加点了菜this.data.foodOrderList', this.data.foodOrderList)
   }, 
   //菜谱上减
   reduceNum: function (e) {
      var i = e.currentTarget.id
      var list = this.data.goodsList
      list[i].num--
      this.setData({ goodsList: list })
      //订单里减
      this.reduceOrder(list[i].Products_ID)
   },

   reduceOrder: function (id) {//订单里减
      var list = this.data.foodOrderList
      for (var i = 0; i < list.length; i++) {
         if (id == list[i].id) {
            if (list[i].num > 1) {
               list[i].num--
            } else {
               list.splice(i, 1)
            }
            break
         }
      }
      this.setData({ foodOrderList: list })
      // console.log('减后订单', list)
      this.countNumAndPrice()
   },

   reduceOnum: function (e) {
      //订单里减
      var i = e.currentTarget.id
      var list = this.data.foodOrderList
      list[i].num--
      this.reduceGoodsList(list[i].id)
      if (list[i].num == 0) {
         list.splice(i, 1)
      }
      this.setData({ foodOrderList: list })
      //菜谱里减
      if (list==0)
      { this.showMenu()}
      
   },

   reduceGoodsList: function (id) {//订单里减
      var list = this.data.goodsList
      for (var i = 0; i < list.length; i++) {
         if (id == list[i].Products_ID) {
            list[i].num--
            break
         }
      }
      this.setData({ goodsList: list })
      console.log('菜谱', list)
      this.countNumAndPrice()
   },

   getOrderFoodsIdArr: function (list) {
      var idArr = []
      for (var i = 0; i < list.length; i++) {
         idArr.push(list[i].id)
      }
      return idArr
   },

   goodsListAddNum: function () {//菜谱上已点的菜加上数量（-）
      var _list = this.data.foodOrderList
      var idArr = this.getOrderFoodsIdArr(_list)
      var list = this.data.goodsList
      for (var i = 0; i < list.length; i++) {
         var index = idArr.indexOf(list[i].Products_ID)
         if (index != -1) {
            list[i].num = _list[index].num
         }
      }
      this.setData({ goodsList: list })
   },
   //底部价格数量显示 
   countNumAndPrice: function () {
      var num = 0
      var price = 0
      var list = this.data.foodOrderList
      for (var i = 0; i < list.length; i++) {
         num += list[i].num
         price += (list[i].price * list[i].num)
      }
      this.setData({
         foodNum: num,
         totalPrice: parseFloat(price).toFixed(2)
      })
   },
   // 已点菜单结算
   toFoodOrder: function () {
      if (this.data.foodOrderList.length == 0) {
         wx.showToast({
            title: '您还没点菜哦',
            image: '/pages/images/qzg_logo_90x.jpg'
         })
         return;
      }
      var tab = this.data.tableInfo
      var obj = {
         command: tab.orderid ? 'add' : 'eat',
         order: this.data.foodOrderList,
         zhuo: tab.Table_ID,
         orderid: tab.orderid,
         guestNum: tab.guestNum,
         fenlei: tab.fenlei,
         zhuohao: tab.zhuo
      }
      let order = JSON.stringify(obj)
      wx.navigateTo({
         url: '/pages/01homePage/03foodOrder/foodOrder?order=' + order,
      })
   },
   //判断点击的index 
   isExistAtOrder: function (goods) {
      var isExist = 0
      var order = this.data.foodOrderList
      for (var i = 0; i < order.length; i++) {
         if (goods.id == order[i].id) {
            isExist++
            return i
         } else if (goods.Products_ID == order[i].id){
            isExist++
            return i
         }
      }
      return -1
   },

   addFoodMenuResult: function (resData) {
      console.log(resData)
      console.log('resData')
   },
   // 获取商家分类数据
   handleCateData: function () {
      // appData.shopInfo.shopInfo.userid  Category_ID
      var param = {
         Biz_ID: 71,
      }
      app.getJsonData('biz/Bmg/getBizCategoryList', param, function (cateData) {
         var c_list = cateData.data
         if (cateData.code == 0) {
            _this.setData({
               goodsCateList: c_list      //页面用分类数据
            })
            appData.goodsCateList = c_list
            // 加载首页执行首项数据加载
            _this.getGoodsData(c_list[0].Category_ID)
            _this.renderingCateBtn(0)
         }
      })
   },
   // 商品分类点击事件
   cateClick: function (e) {
      var index = Number(e.currentTarget.id)
      // 获取商品分类数据
      var sort = this.data.goodsCateList  
      var clickID = sort[index].Category_ID
      //获取下标为index的分类商品列表
      this.getGoodsData(clickID)
      //渲染分类按钮
      this.renderingCateBtn(index)
   },
   //获取商品数据 
   getGoodsData: function (id) {
      var param = {
         Biz_ID: 71,
         pageRows: 20,
         Products_BizCategory: id,
         // uid: appData.shopInfo.shopInfo.userid,
         // cateid: this.data.goodsCateList[index].cateId
      }
      console.log("【获取传递的param】===",param)
      app.getJsonData('product/Pmg/getProductList', param, function (goodsData) {
         _this.setData({ goodsList: goodsData.data })
         _this.goodsListAddNum()
      })
   },
   //渲染分类按钮
   renderingCateBtn: function (index) {
      var btnStateArr = []
      var num = this.data.goodsCateList.length
      for (var i = 0; i < num; i++) {
         btnStateArr.push(false)
      }
      btnStateArr[index] = true
      this.setData({
         cateBtnStateArr: btnStateArr
      })
   },

   showMenu: function (e) {
      this.setData({
         msg_show: this.data.msg_show ? 0 : 1
      })
      console.log('加点了菜this.data.foodOrderList', this.data.foodOrderList)
   }

})
